package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.FansInfo;
import com.kmzx.one.project.others.dto.UserFansPageInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.others.dto.UserRankMoreInfo;
import com.kmzx.one.project.service.UserFanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

@Api(tags = "快美甄选_[ 粉丝相关_接口 ]")
@RestController
public class UserFansController extends BaseController {

    @Autowired
    UserFanService userFanService;


    @ApiOperation(value = "1.我的直属粉丝列表", notes = "我的直属粉丝列表")
    @GetMapping(value = {"fans/one","fans/entry"}, produces = "application/json;charset=UTF-8")
    public BaseResult oneFansLists(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") String userId,
                                   String nickName,Integer sorts) {
        try {
            MyAssert.notNullObject(pageNum, "pageNum 为空");
            MyAssert.notNullObject(pageSize, "pageSize 为空");
            MyAssert.notNullObject(userId, "userId 为空");
            sorts = sorts == null ? super.fansSorts : sorts;
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            List<FansInfo> result = userFanService.findUserFans2(userId, pageNum, pageSize,nickName,sorts);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(result);
            return error;

        } catch (Exception e) {
            return handleException(e);
        }
    }



    @ApiOperation(value = "4.我的管家", notes = "我的管家")
    @GetMapping(value = "fans/manager", produces = "application/json;charset=UTF-8")
    public BaseResult getMyInvitedUser(@RequestParam("userId") String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为空");
            UserRankMoreInfo myManager = userFanService.findMyManager(userId);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(myManager);
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    //@ApiOperation(value = "5.我的全部粉丝", notes = "我的全部粉丝")
    //@GetMapping(value = "fans/entry", produces = "application/json;charset=UTF-8")
    public BaseResult allFansLists2(@RequestParam("userId") String userId, @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("pageNum") Integer pageNum,Integer sorts) {
        try {
            sorts = sorts == null ? super.fansSorts : sorts;
            MyAssert.notNullObject(userId, "userId 为空");
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            List<FansInfo> infos = userFanService.findUserFansEntry2(userId, pageNum, pageSize,sorts);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(infos);
            return error;
        } catch (Exception e) {
            return handleException(e);
        }


    }


    //@ApiOperation(value = "6.我的粉丝_搜索功能", notes = "我的粉丝_搜索功能")
    //@GetMapping(value = "fans/entrySearch", produces = "application/json;charset=UTF-8")
    public BaseResult searchFansByName(String nickName, String userId, Integer pageSize, Integer pageNum,Integer sorts) {
        try {
            MyAssert.notNullObject(userId, "userId 为空");
            MyAssert.notNullObject(nickName, "nickName 为空");
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            sorts = sorts == null ? super.fansSorts : sorts;
            UserFansPageInfo infos = userFanService.searchUserFansByNickName2(nickName, userId, pageNum, pageSize,sorts);
            long total = infos.getTotal();
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(null);
            if(total!=0){
                error.setData(infos.getChildren());
            }
            return error;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "7.我的直属粉丝__搜索功能", notes = "我的直属粉丝__搜索功能")
    @GetMapping(value = {"fans/oneSearch","fans/entrySearch"}, produces = "application/json;charset=UTF-8")
    public BaseResult oneFansListSearch(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") String userId,
                                   String nickName,Integer sorts) {
        try {
            MyAssert.notNullObject(pageNum, "pageNum 为空");
            MyAssert.notNullObject(pageSize, "pageSize 为空");
            MyAssert.notNullObject(userId, "userId 为空");
            sorts = sorts == null ? super.fansSorts : sorts;
            List<FansInfo> result = userFanService.findUserFansSearch2(userId, pageNum, pageSize,nickName,sorts);
            BaseResult error = BaseResult.Builder.SUCC().initSuccData(result);
            return error;

        } catch (Exception e) {
            return handleException(e);
        }
    }
}
